package com.it.glide.glidesample;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;

public class GlideListActivity extends AppCompatActivity {

    private ListView listView;
    private String[] IMAGE_URLS = {
            "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3109554851,495120734&fm=200&gp=0.jpg",
            "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2070583878,1749520961&fm=27&gp=0.jpg",
            "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2251229544,2329383524&fm=200&gp=0.jpg",
            "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1198570369,2091944166&fm=200&gp=0.jpg",
            "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=4102954597,1931058219&fm=200&gp=0.jpg",
            "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3130094236,4160746723&fm=200&gp=0.jpg",
            "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3750997134,3022295709&fm=200&gp=0.jpg",
            "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2208715725,3447775989&fm=200&gp=0.jpg",
            "http://i.imgur.com/aC9OjaM.jpg",
            "http://i.imgur.com/76Jfv9b.jpg",
            "http://i.imgur.com/fUX7EIB.jpg",
            "http://i.imgur.com/syELajx.jpg",
            "http://i.imgur.com/COzBnru.jpg",
            "http://i.imgur.com/1YKvDg7b.jpg",
            "http://i.imgur.com/RRrUqaab.jpg",
            "https://i.imgur.com/F593HLwb.jpg",
            "https://i.imgur.com/R9Vk9lhb.jpg",
            "http://i.imgur.com/Z3QjilA.jpg",
            "https://i.imgur.com/7irmUZpb.jpg",
            "https://i.imgur.com/h2jiwFdb.jpg"

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        listView = (ListView) findViewById(R.id.lv_glide);
        listView.setAdapter(new ImageAdapter(GlideListActivity.this));

    }


    private class ImageAdapter extends BaseAdapter {


        private LayoutInflater inflater;


        ImageAdapter(Context context) {
            inflater = LayoutInflater.from(context);

        }

        @Override
        public int getCount() {
            return 20;
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View view = convertView;
            final ViewHolder holder;
            if (convertView == null) {
                view = inflater.inflate(R.layout.item_list_image, parent, false);
                holder = new ViewHolder();
                holder.text = (TextView) view.findViewById(R.id.text);
                holder.image = (ImageView) view.findViewById(R.id.image);
                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }

            holder.text.setText("Item " + (position + 1));

//            ImageLoader.getInstance().displayImage(IMAGE_URLS[position], holder.image, options, animateFirstListener);
            Glide.with(GlideListActivity.this)
                    .load(IMAGE_URLS[position])
                    .skipMemoryCache(true)
                    .listener(new RequestListener<String, GlideDrawable>() {
                        @Override
                        public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                            return false;
                        }

                        @Override
                        public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                          /*  GlideBitmapDrawable bd   = (GlideBitmapDrawable) imageView.getDrawable();
                            if (bd != null) {
                                Bitmap image  =    bd.getBitmap();
                                System.out.println("__height:"+image.getHeight()+"width:"+image.getWidth());
                            }*/
                            target.getClass();
                            return false;
                        }



                    }).
                    diskCacheStrategy(DiskCacheStrategy.NONE)
//.override(300,300)
                    .dontAnimate()
                    .fitCenter()
                    .into(holder.image);

//            Glide.with(GlideListActivity.this).load(new WebServiceGlideUrl(""))

            return view;
        }
    }

    static class ViewHolder {
        TextView text;
        ImageView image;
    }


}
